package com.neu.servlet.snn;

import com.alibaba.fastjson.JSONObject;
import com.neu.service.snn.UserService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;

@WebServlet(name = "AddAddressServlet", urlPatterns = {"/AddAddressServlet.do"})
public class AddAddressServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String u_id = request.getParameter("u_id");
        String a_add = request.getParameter("a_add");
        String a_name = request.getParameter("a_name");
        String a_tel = request.getParameter("a_tel");
        UserService userService = new UserService();
        boolean flag = userService.addAddress(u_id,a_name,a_add,a_tel);
        Map<String,Object> map=new HashMap<>();
        if (flag){
            map.put("msg","添加成功");
            String json=JSONObject.toJSONString(map);
            System.out.println(json);
            PrintWriter out=response.getWriter();
            out.print(json);
            out.close();
        }else {
            map.put("msg","添加成功");
            String json = JSONObject.toJSONString(map);
            PrintWriter out = response.getWriter();
            out.print(json);
            out.close();
        }
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }
}
